package com.woniuxy.controller;

import com.woniuxy.entity.*;
import com.woniuxy.service.MessageService;
import com.woniuxy.service.SigningService;
import com.woniuxy.utils.CodeUtils;
import com.woniuxy.utils.LoginRegister;
import com.woniuxy.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/signing")
public class SigningController {

    @Autowired
    private SigningService signingService;


    /**
     * 根据教练ID查询他签约了哪些场馆
     * @param coachId
     * @return
     */
    @GetMapping("/myVenues/{coachId}")
    public ResponseEntity<List<Venue>> findVenuesByCoachId(@PathVariable("coachId") int coachId){
        ResponseEntity<List<Venue>> venueResponseEntity = new ResponseEntity<>();
        List<Venue> venuesByCoachId = signingService.findVenuesByCoachId(coachId);
        if (venuesByCoachId.size()<1){
            return venueResponseEntity.setCode(CodeUtils.ERROR_CODE).setMsg("该教练还没有签约场馆");
        }
        return venueResponseEntity.setCode(CodeUtils.SUCCESS_CODE)
                .setMsg("该教练的场馆们").setData(venuesByCoachId);
    }

    /**
     * 根据场馆id查询签约的教练有哪些
     * @param venueId 场馆id
     * @return 签约的所有教练
     */
    @GetMapping("/myCoaches/{venueId}")
    public ResponseEntity<List<Coach>> findCoachesByVenueId(@PathVariable("venueId") int venueId){
        ResponseEntity<List<Coach>> coachListResponseEntity = new ResponseEntity<>();
        System.out.println("教练：");
        System.out.println(venueId);
        List<Coach> coachesByVenueId = signingService.findCoachesByVenueId(venueId);
        if (coachesByVenueId!=null){
            return coachListResponseEntity.setMsg("你的教练们").setCode(CodeUtils.SUCCESS_CODE)
                    .setData(coachesByVenueId);
        }
        return coachListResponseEntity.setCode(CodeUtils.ERROR_CODE).setMsg("出大问题");
    }

    /**
     * 解约一个教练
     * @param signing 签约表实例
     * @return ResponseEntity
     */
    @DeleteMapping("/fire")
    public ResponseEntity<Void> fireCoach(@RequestBody Signing signing){
        if (signing==null){
            return ResponseEntity.ERROR.setMsg("参数为空");
        }
        if (signingService.fireCoach(signing)== LoginRegister.OPERATION_SUCCESS) {
            return ResponseEntity.SUCCESS.setMsg("解约成功");
        }
        return ResponseEntity.ERROR.setMsg("服务器出错了");
    }

    /**
     * 接受签约
     * @param signing
     * @return
     */
    @PostMapping("/accept")
    public ResponseEntity<Void> acceptSigning(@RequestBody Signing signing){
        if (signing==null){
            return ResponseEntity.ERROR.setMsg("参数为空");
        }
        if (signing.getCid()==0||signing.getVid()==0){
            return ResponseEntity.ERROR.setMsg("参数有误");
        }
        switch (signingService.addNewSigning(signing)){
            case LoginRegister.OPERATION_SUCCESS:  return ResponseEntity.SUCCESS.setMsg("签约成功");
            case CodeUtils.SIGNING_EXIST: return ResponseEntity.ERROR.setMsg("你们已经签约了");
        }
        return ResponseEntity.ERROR.setMsg("签约失败");
    }


}
